package com.zcf.service;

import com.zcf.pojo.Activity;
import com.zcf.vo.out.LayuiJson;
import com.zcf.common.json.Body;
import com.zcf.mapper.ActivityMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-06-15
 */
@Service
@SuppressWarnings("unused")
public class ActivityServiceImpl {
	@Autowired
	ActivityMapper activityMapper;
	
	/**
	 * 添加活动
	 * @param activity
	 * @return
	 */
	public Body add(Activity activity) {
		if(activity.getTitle() !="" && activity.getContent() != ""){
			boolean b = activity.insert();
			if(b){
				return Body.BODY_200;
			}
			
		}
		return Body.BODY_401;
	}
	/**
	 * 获取活动
	 * @param activity
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */
	
	public LayuiJson getactivity(Activity activity,int pagenum,int pagesize) {
		
		LayuiJson lj = new LayuiJson();
		EntityWrapper<Activity> w = new EntityWrapper<Activity>();
		int count = activity.selectCount(w);
		List<Activity> list = activityMapper.selectPage(new Page<>(pagenum, pagesize), w);
		if (list == null || list.size()==0) {
			lj.setCode(1);
			lj.setMsg("无数据");
			lj.setCount(count);
			lj.setData(null);
			return lj;
		}
		lj.setCode(0);
		lj.setMsg("yes");
		lj.setCount(count);
		lj.setData(list);
		return lj;
	}
	/**
	 * 修改活动
	 * @param activity
	 * @return
	 */
	public Body editactivity(Activity activity) {
		if(activity.getId()!=null){
			Activity selectById = activity.selectById();
			if(activity.getTitle()!=""){
				selectById.setTitle(activity.getTitle());
			}
			if(activity.getContent() != ""){
				selectById.setContent(activity.getContent());
			}
			boolean b = selectById.updateById();
			if(b){
				return Body.BODY_200;
			}
		}
		
		return Body.BODY_401;
	}
	/**
	 * 删除活动
	 * @param activity
	 * @return
	 */
	public Body delactivity(Activity activity) {
		if(activity.getId()!=null) {
			boolean b = activity.deleteById();
			if(b){
				return Body.BODY_200;
			}
		}
		return Body.BODY_401;
	}
	/**
	 * 获取活动标题
	 * @param activity
	 * @return
	 */
	public Body gettitle(Activity activity) {
		Map<String, String>map = null;
		List<Map<String, String>> lists = new ArrayList<Map<String,String>>();
		List<Activity> list = activity.selectList(new EntityWrapper<Activity>().setSqlSelect("title"));
		for (Activity activity2 : list) {
			map = new HashMap<String, String>();
			map.put("title", activity2.getTitle());
			lists.add(map);
		}
		if(lists!= null){
			return Body.newInstance(lists);
		}
		return Body.BODY_401;
	}
	/**
	 * 根据标题获取内容
	 * @param activity
	 * @return
	 */
	public Body getcontent(Activity activity) {
		
		if(activity.getTitle()!= ""){
			Activity activity2 = activity.selectOne(new EntityWrapper<Activity>().eq("title", activity.getTitle()));
			if(activity2!=null){
				Map<String, String> map = new HashMap<String, String>();
				map.put("content", activity2.getContent());
				if(map!=null){
					return Body.newInstance(map);
				}
			}
		}
		return Body.BODY_401;
	}

}
